package com.wx.service.accesstoken.service.impl;

import com.wx.service.accesstoken.service.IAccessTokenService;
import com.wx.service.accesstoken.vo.AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


@Service
public class AccessTokenServiceImpl implements IAccessTokenService {

  @Value("${wx.accesstoken.url}")
  private String accessTokenUrl;

  @Autowired
  private RestTemplate restTemplate;

  /**
   * 参数	说明
   * errcode	出错返回码，为0表示成功，非0表示调用失败
   * errmsg	返回码提示语
   * access_token	获取到的凭证，最长为512字节
   * expires_in	凭证的有效时间（秒）
   *
   * @return
   */
  @Override
  public String getAccessToken() {
    AccessToken accessToken = restTemplate.getForObject(accessTokenUrl, AccessToken.class);
    if (0 == accessToken.getErrcode()) {
      return accessToken.getAccess_token();
    } else {
      throw new RuntimeException("获取agent access token 失败：" + accessToken.getErrmsg());
    }
  }
}
